package org.example.musicserver.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.musicserver.common.Result;
import org.example.musicserver.entity.PlayList;
import org.example.musicserver.entity.PlayListComment;
import org.example.musicserver.entity.Song;
import org.example.musicserver.entity.User;
import org.example.musicserver.entity.VO.PlayListCommentVO;
import org.example.musicserver.mapper.PlayListCommentMapper;
import org.example.musicserver.mapper.PlayListMapper;
import org.example.musicserver.mapper.SongMapper;
import org.example.musicserver.mapper.UserMapper;
import org.example.musicserver.service.PlayListCommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class PlayListCommentServiceImpl extends ServiceImpl<PlayListCommentMapper, PlayListComment> implements PlayListCommentService {
    @Resource
    private PlayListCommentMapper playListCommentMapper;

    @Resource
    private PlayListMapper playListMapper;

    @Resource
    private SongMapper songMapper;

    @Resource
    private UserMapper userMapper;
    @Override
    public List<PlayListCommentVO> commentOfPlayListId(Integer playListId,Byte type){
        QueryWrapper<PlayListComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("play_list_id",playListId).eq("type",type).orderByDesc("create_time");
        List<PlayListComment> playListComments = playListCommentMapper.selectList(queryWrapper);
        List<User> users = userMapper.selectList(null);

        Map<Integer, User> uidMap = users.stream().collect(Collectors.toMap(User::getId, Function.identity()));

        List<PlayListCommentVO> playListCommentVOS = new ArrayList<>();
        for (PlayListComment playListComment : playListComments) {
            PlayListCommentVO playListCommentVO = new PlayListCommentVO();
            BeanUtils.copyProperties(playListComment,playListCommentVO);
            User user = uidMap.getOrDefault(playListComment.getUserId(),new User());
            playListCommentVO.setUserName(user.getName());
            playListCommentVO.setAvatar(user.getAvatar());
            playListCommentVOS.add(playListCommentVO);
        }
        return playListCommentVOS;
    }
    @Override
    public Result addPlayListComment(Integer userId, Integer playListId, String content,Byte type){
        PlayListComment comment = new PlayListComment();
        comment.setUserId(userId);
        comment.setPlayListId(playListId);
        comment.setContent(content);
        comment.setType(type);
        if (playListCommentMapper.insert(comment) > 0) {
            return Result.success("添加评论", true);
        } else {
            return Result.error("添加评论失败");
        }
    }

    @Override
    public Result deletePlayListComment(Integer id,Byte type){
        QueryWrapper<PlayListComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id).eq("type",type);
        if (playListCommentMapper.delete(queryWrapper) > 0) {
            return Result.success("删除评论成功", true);
        } else {
            return Result.error("删除评论失败");
        }
    }
    @Override
    public Result commentOfAllPlayList(Byte type,String query){
        QueryWrapper<PlayListComment> playListCommentQueryWrapper = new QueryWrapper<>();
        playListCommentQueryWrapper.eq("type",type);
        if(query != null){
            playListCommentQueryWrapper.like("content",query);
        }
        List<PlayListComment> playListComments = playListCommentMapper.selectList(playListCommentQueryWrapper);

        List<PlayListCommentVO> playListCommentVOS = new ArrayList<>();
        if(playListComments.isEmpty()){
            return Result.success("",playListCommentVOS);
        }

        List<User> users = userMapper.selectList(null);

        Map<Integer, User> uidMap = users.stream().collect(Collectors.toMap(User::getId, Function.identity()));

        Set<Integer> ids = playListComments.stream().map(PlayListComment::getPlayListId).collect(Collectors.toSet());
        Map<Integer, String> playListIdMap = new HashMap<>();
        Map<Integer, String> songIdMap = new HashMap<>();
        if(type == 1){
            QueryWrapper<PlayList> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id",ids);
            playListIdMap = playListMapper.selectList(queryWrapper).stream().collect(Collectors.toMap(PlayList::getId, PlayList::getTitle));
        }else {
            QueryWrapper<Song> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id",ids);
            songIdMap = songMapper.selectList(queryWrapper).stream().collect(Collectors.toMap(Song::getId, Song::getName));
        }

        for (PlayListComment playListComment : playListComments) {
            PlayListCommentVO playListCommentVO = new PlayListCommentVO();
            BeanUtils.copyProperties(playListComment,playListCommentVO);
            User user = uidMap.getOrDefault(playListComment.getUserId(),new User());
            playListCommentVO.setUserName(user.getName());
            playListCommentVO.setAvatar(user.getAvatar());
            playListCommentVO.setSongName(songIdMap.get(playListComment.getPlayListId()));
            playListCommentVO.setPlayListName(playListIdMap.get(playListComment.getPlayListId()));
            playListCommentVOS.add(playListCommentVO);
        }
        return Result.success(null, playListCommentVOS);
    }
}
